package com.college.eduService.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.college.commonutil.exceptionhandler.GuliException;
import com.college.commonutil.result.Result;
import com.college.eduService.client.VodClient;
import com.college.eduService.entity.EduVideo;
import com.college.eduService.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description="课程小节管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/video")
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;
    @Autowired
    VodClient vodClient;

    //添加小节
    @PostMapping("saveVideo")
    @ApiOperation(value="添加小节")
    private Result saveVideo(@RequestBody EduVideo video){
        boolean save = videoService.save(video);
        if(save){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
    //根据id查询小节
    @ApiOperation(value = "根据小节id查询")
    @GetMapping("getVideoInfo/{id}")
    public Result getVideoInfo(@PathVariable String id){
        EduVideo eduVideo = videoService.getById(id);
        return Result.ok().data("eduVideo",eduVideo);
    }
    //删除小节
    @DeleteMapping("del/{id}")
    @ApiOperation(value="删除小节")
    private Result delVideo(@PathVariable String id){
        EduVideo video=videoService.getById(id);
        vodClient.removeAlyVideo(video.getVideoSourceId());
        boolean b = videoService.removeById(id);
        if(b){
            return  Result.ok();
        }else {
            return  Result.error();
        }
    }
    //修改小节
    @PostMapping("updateVideo")
    @ApiOperation(value="修改小节")
    private Result updateVideo(@RequestBody EduVideo video){
        boolean save = videoService.updateById(video);
        if(save){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
    //根据云端id查询小节
    @GetMapping("getVideoInfoBySourse/{id}")
    public EduVideo getVideoInfoBySourse(@PathVariable String id){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("video_source_id",id);
        EduVideo eduVideo = videoService.getOne(wrapper);
        return eduVideo;
    }
}
